package 每日一题;

public class 把字符串转换成整数 {
    public int StrToInt(String str) {
        if (str==null || str.length()==0) {
            return 0;
        }

        char[] arr = str.toCharArray();
        int res=0;
        int fuhao=0;
        if (arr[0]=='-') {
            fuhao=1;
        }
        for (int i = fuhao; i <str.length(); i++) {
            if (arr[i]=='+') {
                continue;
            }
            if (arr[i]<48 || arr[i]>57) {
                //48为字符0的ASCII值
                //57为字符9的ASCII值
                return 0;
            }else {
                res = res*10+arr[i]-48;
                //res*10+arr[i]-'0'
            }
        }

        return fuhao==1?res*-1:res;

    }
}
